package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.dao.WQbdSoftwareproAnlsDao;
import com.ccp.dev.qbdms.model.WQbdQualityAnalysisReport;
import com.ccp.dev.qbdms.model.WQbdSoftwareproAnls;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.qbdms.util.ReadAttachmentUtil;
import java.util.List;
import javax.annotation.Resource;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 软件问题分析 信息操作服务类
 *
 * @author wyl
 * @date 2020-07-06 09:55:37
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WQbdSoftwareproAnlsService extends BaseService<WQbdSoftwareproAnls> {

    @Resource
    private WQbdSoftwareproAnlsDao dao;

    public HSSFWorkbook exportExcel(Excel excel,QueryFilter queryFilter){
    List<WQbdSoftwareproAnls> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (WQbdSoftwareproAnls data : dataList) {
        excel.cell(startRow, 0).value(data.getId());
        //excel.cell(startRow, 1).value(data.getMc());
        //可在赋值时，根据需求进行格式化
        //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
        //excel.cell(startRow, 3).value(data.getSj());
        //excel.cell(startRow, 4).value(data.getTenantId());
        startRow++;
        }
        return excel.getWorkBook();
    }

    public List<WQbdQualityAnalysisReport> reportList(String id, String tableName) {
        return dao.reportList(id, tableName);
    }

    public List<WQbdSoftwareproAnls> queryAll(QueryFilter queryFilter) {
        return dao.queryAll(queryFilter);
    }

    /**
     * 导入新增
     * @param list  数据集
     * @throws Exception 异常
     */
    public void addImportData(List<WQbdSoftwareproAnls> list) {
        list.forEach(a->{
            a.setId(ProjectUUIDUtils.getUUIDFor32());
            ZhiLiangConst.addCommonFields(a);
            a.setReportStatus(ZhiLiangConst.getDataInitStatusToHospital());
        });
        if(!list.isEmpty()){
            dao.addList(list);
        }
    }

    /**
     * 根据主表id查询主表加子表数据
     *
     * @param id 主表id
     * @return
     */
    public WQbdSoftwareproAnls getParentAndChilden(String id) {
        return dao.getParentAndChilden(id);
    }

    /**
     * 保存接口传输的数据
     *
     * @param wQbdSoftwareproAnlsList 主表model,有子表的包含子表数据
     * @return
     */
    public int saveAll(List<WQbdSoftwareproAnls> wQbdSoftwareproAnlsList,List<ExtendSysFile> extendSysFileList){
        ReadAttachmentUtil.getEntityData(wQbdSoftwareproAnlsList,extendSysFileList);
        int result=0;
        for (WQbdSoftwareproAnls wQbdSoftwareproAnls:wQbdSoftwareproAnlsList
             ) {
            if(StringUtil.isEmpty(wQbdSoftwareproAnls.getFilldeptHospitalId())){
                wQbdSoftwareproAnls.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(wQbdSoftwareproAnls.getFilldeptid()));
            }else{
                wQbdSoftwareproAnls.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(wQbdSoftwareproAnls.getFilldeptHospitalId()));
            }
            if(null==dao.getById(wQbdSoftwareproAnls.getId())){
                result+=dao.add(wQbdSoftwareproAnls);
            }else {
                result+=dao.update(wQbdSoftwareproAnls);
            }
        }
        return result;
    }
}
